package com.water.utils;

import com.water.dao.BeOrderDao;
import com.water.service.BeOrderService;
import com.water.service.impl.BeOrderServiceImpl;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.joda.time.LocalDateTime;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.NumberUtils;

import javax.annotation.Resource;
import javax.enterprise.deploy.shared.CommandType;
import java.awt.print.Book;
import java.io.InputStream;
import java.lang.management.ManagementFactory;
import java.text.SimpleDateFormat;
import java.util.Date;
//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:/config/spring-mybatis.xml")
@Component
public class Util {
    @Resource
    private BeOrderService beOrderService;


    // 生成工单号
    public String getOrderCode(Integer ordertype) {
        String str = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String ord = beOrderService.queryMaxOrd("B"+ordertype);
        Integer o;
        if (ord != null) {
            o = Integer.parseInt(ord);
            o++;
        } else {
            o = 1;
        }
        String lsh = "";
        if (o < 10) {
            lsh = "B" + ordertype + "-" + str + "-000" + o;
        } else if (o >= 10 && o <= 99) {
            lsh = "B" + ordertype + "-" + str + "-00" + o;
        } else if (o >= 100 && o <= 999) {
            lsh = "B" + ordertype + "-" + str + "-0" + o;
        }
        return lsh;
    }
    //生成用户编码
    public String getusno(Integer areaid){
        String usN = beOrderService.queryMaxUsNo(areaid);
        Integer e;
        if (usN != null) {
            e = Integer.parseInt(usN);
            e++;
        }else {
            e=1;
        }
        String lsh="";
        if (e < 10&&areaid<10) {
            lsh="0"+areaid+"0000000"+e;
        }else if (areaid<10&&e>=10&&e<100) {
            lsh="0"+areaid+"000000"+e;
        }else if (areaid<10&&e>=100&&e<1000) {
            lsh="0"+areaid+"00000"+e;
        }else if (areaid<10&&e>=1000&&e<10000) {
            lsh="0"+areaid+"0000"+e;
        }else if (areaid<10&&e>=10000&&e<100000) {
            lsh="0"+areaid+"000"+e;
        }else if (areaid<10&&e>=100000&&e<1000000) {
            lsh="0"+areaid+"00"+e;
        }else if (areaid<10&&e>=1000000&&e<10000000) {
            lsh="0"+areaid+"0"+e;
        }if (areaid<10&&e==10000000) {
            lsh="0"+areaid+e;
        }else if (e<10) {
            lsh=areaid+"0000000"+e;
        }else if (e<100) {
            lsh=areaid+"000000"+e;
        }else if (e<1000) {
            lsh=areaid+"00000"+e;
        }else if (e<10000) {
            lsh=areaid+"0000"+e;
        }else if (e<100000) {
            lsh=areaid+"000"+e;
        }else if (e<1000000) {
            lsh=areaid+"00"+e;
        }else if (e<10000000) {
            lsh=areaid+"0"+e;
        }else if (e==10000000) {
            lsh= areaid+e+"";
        }

        return lsh;

    }
    //发送
    public Integer Setpid(Integer stepid,Integer ordertype){
        switch (ordertype){
            case 1://新户
                if (stepid == 3) {
                    stepid = stepid+2;
                }else {
                    stepid++;
                }
                break;
            case 2://分户
                stepid++;
                break;
            case 3://过户
            case 4://代扣
                if (stepid == 2) {
                    stepid = stepid+6;
                }else{
                    stepid++;
                }
                break;
            case 5://换表
                if (stepid == 3) {
                    stepid = stepid+3;
                }else {
                    stepid++;
                }
                break;
            case 6://重签
                if (stepid == 2) {
                    stepid = stepid+3;
                    break;
                }
                if (stepid == 5) {
                    stepid = stepid+2;
                    break;
                }
                else {
                    stepid++;
                }
                break;
            case 7://销户
                stepid++;
                if (stepid == 2) {
                    stepid = stepid+2;
                    break;
                }
                if (stepid == 4) {
                    stepid = stepid+3;
                }else {
                    stepid++;
                }
                break;
                default:
                    stepid++;
                    break;
        }
        return stepid;
    }
    //生成当前日期
    @Test
    public Date CreateDate(){
        Date date = new Date();
        return date;
    }
    //撤回
    public Integer Setpid_bk(Integer stepid,Integer ordertype){
        switch (ordertype){
            case 1://新户
                if (stepid == 5) {
                    stepid = stepid-2;
                }else {
                    stepid--;
                    System.out.println(stepid+"opppopo");
                }
                break;
            case 2://分户
                stepid--;
                break;
            case 3://过户
            case 4://代扣
                if (stepid == 8) {
                    stepid = stepid-6;
                }else{
                    stepid--;
                }
                break;
            case 5://换表
                if (stepid == 6) {
                    stepid = stepid-3;
                }else {
                    stepid--;
                }
                break;
            case 6://重签
                if (stepid == 5) {
                    stepid = stepid-3;
                    break;
                }
                if (stepid == 8) {
                    stepid = stepid-3;
                    break;
                }
                else {
                    stepid--;
                }
                break;
            case 7://销户
                stepid++;
                if (stepid == 7) {
                    stepid = stepid-3;
                    break;
                }
                if (stepid == 4) {
                    stepid = stepid-2;
                }else {
                    stepid--;
                }
                break;
            default:
                stepid--;
                break;
        }
        return stepid;
    }



}
